/******************************************************************************
Description: This is the program that (I) installs necessary packages for
replication and (II)-(III) defines and creates the folders relevant to store
the data converted from the raw files, the working data, and the code output.
********************************************************************************/

* User Global
global user `c(username)'

* Set version
version 17

* Set seed
set seed 123456

* (I) Package installation
* (packages used as of date of publication are pre-installed in the /ado subfolder
* of the replication package)
/*
ssc install carryforward
ssc install strdist, replace
ssc install ivreg2, replace
ssc install ranktest, replace
ssc install estout, replace
ssc install unique, replace
ssc install ivreghdfe, replace
ssc install ftools, replace
ssc install reghdfe, replace
*/

** Set person-project-specific adopaths to install needed programs
local ado_dirs  A:/bps_prek/$user/code/ado/plus ///
                A:/bps_prek/$user/code/ado/personal

** Loop over ado directories
foreach dir of local ado_dirs {

  *** Check if directory exists
  mata : st_numscalar("exists", direxists("`dir'"))

  *** If not, create directory
  if scalar(exists) == 0        mkdir "`dir'"
}

** Set system ado folders accordingly
sysdir set PLUS       "A:/bps_prek/$user/code/ado/plus"
sysdir set PERSONAL   "A:/bps_prek/$user/code/ado/personal"
net set ado           "A:/bps_prek/$user/code/ado/plus"

** Install packages

* (II) Set directories
* Set more directories under each type of directory as needed, e.g.
* tables, log files, and decks under Output directories

local data_dir            D:/massachusetts/BPS_PreK/
local result_dir          A:/bps_prek/$user/results/
local project_dir         bps_prek

* Working data directories
local working_dirs  ///
      `data_dir'/$user/ ///
      `data_dir'/$user/analysis_data/ ///
      `data_dir'/$user/analysis_data/nsc/ ///
      `data_dir'/$user/assignment/ ///
      `data_dir'/$user/find_results/ ///
      `data_dir'/$user/location/ ///
      `data_dir'/$user/mcas/ ///
      `data_dir'/$user/nsc/ ///
      `data_dir'/$user/rejoinder/ ///
      `data_dir'/$user/sat/ ///
      `data_dir'/$user/search_request/ ///
      `data_dir'/$user/year_template/ ///
      `data_dir'/$user/head_start/ ///
      `data_dir'/$user/crosswalks/

local sims_subdirs `data_dir'/$user/SIMS/

local crosswalk_subdirs `data_dir'/$user/crosswalks/SASID/

local mcas_subdirs `data_dir'/$user/mcas/

local assign_subdirs `data_dir'/$user/assignment/

local nsc_subdirs `data_dir'/$user/analysis_data/nsc/

forvalues i = 1997/2018 {
  local sims_subdirs `sims_subdirs' ///
      `data_dir'/$user/SIMS/`i'/
  local crosswalk_subdirs `crosswalk_subdirs' ///
      `data_dir'/$user/crosswalks/SASID/`i'/
  local mcas_subdirs `mcas_subdirs' ///
      `data_dir'/$user/mcas/`i'/
  local assign_subdirs `assign_subdirs' ///
      `data_dir'/$user/assignment/`i'/
  local nsc_subdirs `nsc_subdirs' ///
      `data_dir'/$user/analysis_data/nsc/`i'/
}


* Output directories
local output_dirs `result_dir' ///
      `result_dir'/tables/ ///
      `result_dir'/tables/intermediates/ ///
      `result_dir'/tables/joint_f_outcome/ ///
      `result_dir'/tables/joint_f_subgroup/ ///
      `result_dir'/figures/ ///
      `result_dir'/raw_tabs/


* (III) Create directories

** Loop over data and output directories
foreach dir in `working_dirs' `output_dirs' `sims_subdirs' ///
 `crosswalk_subdirs' `mcas_subdirs' `assign_subdirs' ///
 `nsc_subdirs'{

  *** Check if directory exists
  mata : st_numscalar("exists", direxists("`dir'"))

  *** If not, create directory
  if scalar(exists) == 0        mkdir "`dir'"
}
